package com.community.web.user.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.community.model.entity.Notice;
import com.community.model.entity.NoticeRecord;
import com.community.web.user.service.NoticeRecordService;
import com.community.web.user.service.NoticeService;
import com.community.web.user.mapper.NoticeMapper;
import com.community.web.user.vo.NoticeVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
* @author admin
* @description 针对表【notice】的数据库操作Service实现
* @createDate 2024-07-02 10:56:56
*/
@Service
public class NoticeServiceImpl extends ServiceImpl<NoticeMapper, Notice>
    implements NoticeService{
    @Autowired
    private NoticeMapper mapper;

    @Autowired
    private NoticeRecordService recordService;

    @Override
    public IPage<NoticeVo> pageList(Long userId, Page<NoticeVo> page) {
        return mapper.pageList(page, userId);
    }

    @Override
    public Notice getNoticeDetail(long id, Long userId) {
        Notice notice = mapper.selectById(id);
        //获取详情后在记录表添加记录,表示已读
        LambdaQueryWrapper<NoticeRecord> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(NoticeRecord::getNoticeId, id);
        queryWrapper.eq(NoticeRecord::getUserId, userId);
        NoticeRecord record = recordService.getOne(queryWrapper);
        if (record == null){
            NoticeRecord noticeRecord = new NoticeRecord();
            noticeRecord.setNoticeId((int) id);
            noticeRecord.setUserId(userId.intValue());
            recordService.save(noticeRecord);
        }
        return notice;
    }
}




